import {wxRequest} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    pageNo:1, 
    hasNextPage:false,
    showModal:false,
    payDate:'',
    address:[],
    pageData: {}
  },

  onShow: function () { 
    this.getlist();
    // this.getDetail()
  },
  //查询列表
  getlist: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/settle/list',
      method: 'POST',
      data: {
        keyword: this.data.keyword,
        pageNo: this.data.pageNo,
        disId: this.data.address.length > 2 ? this.data.address[2].code : '',
        cityId: this.data.address.length > 1 ? this.data.address[1].code : '',
        proId: this.data.address.length > 0 ? this.data.address[0].code : ''
      }  
    }, (res) => {
      let list = res.list; 
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

      list.map(item=>{
        if (item.payDate){ 
          item.payDateText = item.payDate.substring(0,11)
        }
      })


      this.setData({
        list: list,
        pageData: res.page,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });

      console.log(res,'分润')

    })
  },


  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },


  //再次核算
  checkAgain:function(e){
    var setId = e.currentTarget.dataset.id
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/settle/checkAgain?setId=' + setId,
      method: 'POST',
       
    }, (res) => {
      console.log(res,'再次核算');
      
      wx.showToast({
        icon: 'none',
        title: '已提交再次核算',
      })
     setTimeout(()=>{
       this.getlist();
     },1500)
      
    })
  },


  //付款完成
  overPay: function (e) {
    var setId = e.currentTarget.dataset.id 
    // this.overPayRequset()
    console.log('1111')
    this.setData({
      showModal: !this.data.showModal,
      setId
    })
  },
 


  //取消
  cancel: function () {
    var showModal = this.data.showModal;
    this.setData({
      showModal: !showModal,
      auditMemo: '',
      unAgreedItem: ''
    })
  },

  //确定
  confirm: function () {
    if (!this.data.payDate) {
      wx.showToast({
        icon: 'none',
        title: '请选择付款完成日期',
      })
      return
    }
    // if (!this.data.paytime) {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '请选择付款完成时间',
    //   })
    //   return
    // }
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: "确定执行此操作",
      success: function (res) {
        if (res.confirm) {
          that.setData({
            showModal: false,
          })
          console.log( 'setId:',that.data.setId,'payDate:', that.data.payDate,)
          //参数
          if (that.data.setId) {
            var params = {
              setId: that.data.setId,
              payDate: that.data.payDate, 
            }
            that.overPayRequset(params);
          }
        }
      }
    })
  },


  overPayRequset: function (params){
    console.log(params,'params')
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/settle/overPay',
      contentType: "application/x-www-form-urlencoded",
      method: 'POST',
      data: params,
    }, (res) => {
      this.setData({
        setId: null,
        payDate: null,
      })
      this.getlist()
    })
  },


  bindDateChange:function(e){
    var value=e.detail.value;
    console.log(value,'payDate')
    this.setData({
      payDate:value
    })
  },

  bindTimeChange: function (e) {
    var value = e.detail.value;
    console.log(value, 'paytime')
    this.setData({
      paytime: value
    })
  },

   
  //结算明细
  getDetail: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/settle/detail?setId=98448806567215179',
      method: 'POST'
    }, (res) => {
      console.log(res,'结算明细')

    })
  },


  toDtail: function (e) {
    var id = e.currentTarget.dataset.id;
    var sharedType = e.currentTarget.dataset.sharedtype
    console.log(id, 'id')
    wx.navigateTo({
      url: '/pages/menber/distribut/detail?id=' + e.currentTarget.dataset.id + '&sharedType=' + sharedType,
    })
  },





  //搜索关键词
  seachInputConfirm: function (e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getlist();
  },
  
  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.getlist();
  },

  seachInputVal: function (e) {
    var inputval = e.detail.value;
    console.log(e)
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function () {
    this.setData({
      keyword: ''
    })
    this.getlist();
  },

  //********************筛选更多********************
  popupRight: function () {
    this.setData({
      showPopup: true
    })
  },

  popupCancel: function () {
    this.setData({
      showPopup: false
    })
  },
  //重置
  popupReset: function () {
    this.setData({
      discode: '',
      address: [],
      quyu: false
    })
  },


  popupConfirm: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
    this.popupCancel();
  },


  chooseAddress: function () {
    this.setData({
      quyu: true
    })
  },

  addresscancel: function () {
    this.setData({
      quyu: false
    })
  },

  addressDetail: function (e) {
    this.setData({
      address: e.detail,
      quyu: false
    })
  }

  //********************筛选更多end********************//







})